<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1">

<style type="text/css">
@font-face {
font-family: octicons-link;
src: url(data:font/woff;charset=utf-8;base64,d09GRgABAAAAAAZwABAAAAAACFQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABEU0lHAAAGaAAAAAgAAAAIAAAAAUdTVUIAAAZcAAAACgAAAAoAAQAAT1MvMgAAAyQAAABJAAAAYFYEU3RjbWFwAAADcAAAAEUAAACAAJThvmN2dCAAAATkAAAABAAAAAQAAAAAZnBnbQAAA7gAAACyAAABCUM+8IhnYXNwAAAGTAAAABAAAAAQABoAI2dseWYAAAFsAAABPAAAAZwcEq9taGVhZAAAAsgAAAA0AAAANgh4a91oaGVhAAADCAAAABoAAAAkCA8DRGhtdHgAAAL8AAAADAAAAAwGAACfbG9jYQAAAsAAAAAIAAAACABiATBtYXhwAAACqAAAABgAAAAgAA8ASm5hbWUAAAToAAABQgAAAlXu73sOcG9zdAAABiwAAAAeAAAAME3QpOBwcmVwAAAEbAAAAHYAAAB/aFGpk3jaTY6xa8JAGMW/O62BDi0tJLYQincXEypYIiGJjSgHniQ6umTsUEyLm5BV6NDBP8Tpts6F0v+k/0an2i+itHDw3v2+9+DBKTzsJNnWJNTgHEy4BgG3EMI9DCEDOGEXzDADU5hBKMIgNPZqoD3SilVaXZCER3/I7AtxEJLtzzuZfI+VVkprxTlXShWKb3TBecG11rwoNlmmn1P2WYcJczl32etSpKnziC7lQyWe1smVPy/Lt7Kc+0vWY/gAgIIEqAN9we0pwKXreiMasxvabDQMM4riO+qxM2ogwDGOZTXxwxDiycQIcoYFBLj5K3EIaSctAq2kTYiw+ymhce7vwM9jSqO8JyVd5RH9gyTt2+J/yUmYlIR0s04n6+7Vm1ozezUeLEaUjhaDSuXHwVRgvLJn1tQ7xiuVv/ocTRF42mNgZGBgYGbwZOBiAAFGJBIMAAizAFoAAABiAGIAznjaY2BkYGAA4in8zwXi+W2+MjCzMIDApSwvXzC97Z4Ig8N/BxYGZgcgl52BCSQKAA3jCV8CAABfAAAAAAQAAEB42mNgZGBg4f3vACQZQABIMjKgAmYAKEgBXgAAeNpjYGY6wTiBgZWBg2kmUxoDA4MPhGZMYzBi1AHygVLYQUCaawqDA4PChxhmh/8ODDEsvAwHgMKMIDnGL0x7gJQCAwMAJd4MFwAAAHjaY2BgYGaA4DAGRgYQkAHyGMF8NgYrIM3JIAGVYYDT+AEjAwuDFpBmA9KMDEwMCh9i/v8H8sH0/4dQc1iAmAkALaUKLgAAAHjaTY9LDsIgEIbtgqHUPpDi3gPoBVyRTmTddOmqTXThEXqrob2gQ1FjwpDvfwCBdmdXC5AVKFu3e5MfNFJ29KTQT48Ob9/lqYwOGZxeUelN2U2R6+cArgtCJpauW7UQBqnFkUsjAY/kOU1cP+DAgvxwn1chZDwUbd6CFimGXwzwF6tPbFIcjEl+vvmM/byA48e6tWrKArm4ZJlCbdsrxksL1AwWn/yBSJKpYbq8AXaaTb8AAHja28jAwOC00ZrBeQNDQOWO//sdBBgYGRiYWYAEELEwMTE4uzo5Zzo5b2BxdnFOcALxNjA6b2ByTswC8jYwg0VlNuoCTWAMqNzMzsoK1rEhNqByEyerg5PMJlYuVueETKcd/89uBpnpvIEVomeHLoMsAAe1Id4AAAAAAAB42oWQT07CQBTGv0JBhagk7HQzKxca2sJCE1hDt4QF+9JOS0nbaaYDCQfwCJ7Au3AHj+LO13FMmm6cl7785vven0kBjHCBhfpYuNa5Ph1c0e2Xu3jEvWG7UdPDLZ4N92nOm+EBXuAbHmIMSRMs+4aUEd4Nd3CHD8NdvOLTsA2GL8M9PODbcL+hD7C1xoaHeLJSEao0FEW14ckxC+TU8TxvsY6X0eLPmRhry2WVioLpkrbp84LLQPGI7c6sOiUzpWIWS5GzlSgUzzLBSikOPFTOXqly7rqx0Z1Q5BAIoZBSFihQYQOOBEdkCOgXTOHA07HAGjGWiIjaPZNW13/+lm6S9FT7rLHFJ6fQbkATOG1j2OFMucKJJsxIVfQORl+9Jyda6Sl1dUYhSCm1dyClfoeDve4qMYdLEbfqHf3O/AdDumsjAAB42mNgYoAAZQYjBmyAGYQZmdhL8zLdDEydARfoAqIAAAABAAMABwAKABMAB///AA8AAQAAAAAAAAAAAAAAAAABAAAAAA==) format('woff');
}
body {
-webkit-text-size-adjust: 100%;
text-size-adjust: 100%;
color: #333;
font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 16px;
line-height: 1.6;
word-wrap: break-word;
}
a {
background-color: transparent;
}
a:active,
a:hover {
outline: 0;
}
strong {
font-weight: bold;
}
h1 {
font-size: 2em;
margin: 0.67em 0;
}
img {
border: 0;
}
hr {
box-sizing: content-box;
height: 0;
}
pre {
overflow: auto;
}
code,
kbd,
pre {
font-family: monospace, monospace;
font-size: 1em;
}
input {
color: inherit;
font: inherit;
margin: 0;
}
html input[disabled] {
cursor: default;
}
input {
line-height: normal;
}
input[type="checkbox"] {
box-sizing: border-box;
padding: 0;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
td,
th {
padding: 0;
}
* {
box-sizing: border-box;
}
input {
font: 13px / 1.4 Helvetica, arial, nimbussansl, liberationsans, freesans, clean, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}
a {
color: #4078c0;
text-decoration: none;
}
a:hover,
a:active {
text-decoration: underline;
}
hr {
height: 0;
margin: 15px 0;
overflow: hidden;
background: transparent;
border: 0;
border-bottom: 1px solid #ddd;
}
hr:before {
display: table;
content: "";
}
hr:after {
display: table;
clear: both;
content: "";
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin-top: 15px;
margin-bottom: 15px;
line-height: 1.1;
}
h1 {
font-size: 30px;
}
h2 {
font-size: 21px;
}
h3 {
font-size: 16px;
}
h4 {
font-size: 14px;
}
h5 {
font-size: 12px;
}
h6 {
font-size: 11px;
}
blockquote {
margin: 0;
}
ul,
ol {
padding: 0;
margin-top: 0;
margin-bottom: 0;
}
ol ol,
ul ol {
list-style-type: lower-roman;
}
ul ul ol,
ul ol ol,
ol ul ol,
ol ol ol {
list-style-type: lower-alpha;
}
dd {
margin-left: 0;
}
code {
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 12px;
}
pre {
margin-top: 0;
margin-bottom: 0;
font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
.select::-ms-expand {
opacity: 0;
}
.octicon {
font: normal normal normal 16px/1 octicons-link;
display: inline-block;
text-decoration: none;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.octicon-link:before {
content: '\f05c';
}
.markdown-body:before {
display: table;
content: "";
}
.markdown-body:after {
display: table;
clear: both;
content: "";
}
.markdown-body>*:first-child {
margin-top: 0 !important;
}
.markdown-body>*:last-child {
margin-bottom: 0 !important;
}
a:not([href]) {
color: inherit;
text-decoration: none;
}
.anchor {
display: inline-block;
padding-right: 2px;
margin-left: -18px;
}
.anchor:focus {
outline: none;
}
h1,
h2,
h3,
h4,
h5,
h6 {
margin-top: 1em;
margin-bottom: 16px;
font-weight: bold;
line-height: 1.4;
}
h1 .octicon-link,
h2 .octicon-link,
h3 .octicon-link,
h4 .octicon-link,
h5 .octicon-link,
h6 .octicon-link {
color: #000;
vertical-align: middle;
visibility: hidden;
}
h1:hover .anchor,
h2:hover .anchor,
h3:hover .anchor,
h4:hover .anchor,
h5:hover .anchor,
h6:hover .anchor {
text-decoration: none;
}
h1:hover .anchor .octicon-link,
h2:hover .anchor .octicon-link,
h3:hover .anchor .octicon-link,
h4:hover .anchor .octicon-link,
h5:hover .anchor .octicon-link,
h6:hover .anchor .octicon-link {
visibility: visible;
}
h1 {
padding-bottom: 0.3em;
font-size: 2.25em;
line-height: 1.2;
border-bottom: 1px solid #eee;
}
h1 .anchor {
line-height: 1;
}
h2 {
padding-bottom: 0.3em;
font-size: 1.75em;
line-height: 1.225;
border-bottom: 1px solid #eee;
}
h2 .anchor {
line-height: 1;
}
h3 {
font-size: 1.5em;
line-height: 1.43;
}
h3 .anchor {
line-height: 1.2;
}
h4 {
font-size: 1.25em;
}
h4 .anchor {
line-height: 1.2;
}
h5 {
font-size: 1em;
}
h5 .anchor {
line-height: 1.1;
}
h6 {
font-size: 1em;
color: #777;
}
h6 .anchor {
line-height: 1.1;
}
p,
blockquote,
ul,
ol,
dl,
table,
pre {
margin-top: 0;
margin-bottom: 16px;
}
hr {
height: 4px;
padding: 0;
margin: 16px 0;
background-color: #e7e7e7;
border: 0 none;
}
ul,
ol {
padding-left: 2em;
}
ul ul,
ul ol,
ol ol,
ol ul {
margin-top: 0;
margin-bottom: 0;
}
li>p {
margin-top: 16px;
}
dl {
padding: 0;
}
dl dt {
padding: 0;
margin-top: 16px;
font-size: 1em;
font-style: italic;
font-weight: bold;
}
dl dd {
padding: 0 16px;
margin-bottom: 16px;
}
blockquote {
padding: 0 15px;
color: #777;
border-left: 4px solid #ddd;
}
blockquote>:first-child {
margin-top: 0;
}
blockquote>:last-child {
margin-bottom: 0;
}
table {
display: block;
width: 100%;
overflow: auto;
word-break: normal;
word-break: keep-all;
}
table th {
font-weight: bold;
}
table th,
table td {
padding: 6px 13px;
border: 1px solid #ddd;
}
table tr {
background-color: #fff;
border-top: 1px solid #ccc;
}
table tr:nth-child(2n) {
background-color: #f8f8f8;
}
img {
max-width: 100%;
box-sizing: content-box;
background-color: #fff;
}
code {
padding: 0;
padding-top: 0.2em;
padding-bottom: 0.2em;
margin: 0;
font-size: 85%;
background-color: rgba(0,0,0,0.04);
border-radius: 3px;
}
code:before,
code:after {
letter-spacing: -0.2em;
content: "\00a0";
}
pre>code {
padding: 0;
margin: 0;
font-size: 100%;
word-break: normal;
white-space: pre;
background: transparent;
border: 0;
}
.highlight {
margin-bottom: 16px;
}
.highlight pre,
pre {
padding: 16px;
overflow: auto;
font-size: 85%;
line-height: 1.45;
background-color: #f7f7f7;
border-radius: 3px;
}
.highlight pre {
margin-bottom: 0;
word-break: normal;
}
pre {
word-wrap: normal;
}
pre code {
display: inline;
max-width: initial;
padding: 0;
margin: 0;
overflow: initial;
line-height: inherit;
word-wrap: normal;
background-color: transparent;
border: 0;
}
pre code:before,
pre code:after {
content: normal;
}
kbd {
display: inline-block;
padding: 3px 5px;
font-size: 11px;
line-height: 10px;
color: #555;
vertical-align: middle;
background-color: #fcfcfc;
border: solid 1px #ccc;
border-bottom-color: #bbb;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #bbb;
}
.pl-c {
color: #969896;
}
.pl-c1,
.pl-s .pl-v {
color: #0086b3;
}
.pl-e,
.pl-en {
color: #795da3;
}
.pl-s .pl-s1,
.pl-smi {
color: #333;
}
.pl-ent {
color: #63a35c;
}
.pl-k {
color: #a71d5d;
}
.pl-pds,
.pl-s,
.pl-s .pl-pse .pl-s1,
.pl-sr,
.pl-sr .pl-cce,
.pl-sr .pl-sra,
.pl-sr .pl-sre {
color: #183691;
}
.pl-v {
color: #ed6a43;
}
.pl-id {
color: #b52a1d;
}
.pl-ii {
background-color: #b52a1d;
color: #f8f8f8;
}
.pl-sr .pl-cce {
color: #63a35c;
font-weight: bold;
}
.pl-ml {
color: #693a17;
}
.pl-mh,
.pl-mh .pl-en,
.pl-ms {
color: #1d3e81;
font-weight: bold;
}
.pl-mq {
color: #008080;
}
.pl-mi {
color: #333;
font-style: italic;
}
.pl-mb {
color: #333;
font-weight: bold;
}
.pl-md {
background-color: #ffecec;
color: #bd2c00;
}
.pl-mi1 {
background-color: #eaffea;
color: #55a532;
}
.pl-mdr {
color: #795da3;
font-weight: bold;
}
.pl-mo {
color: #1d3e81;
}
kbd {
display: inline-block;
padding: 3px 5px;
font: 11px Consolas, "Liberation Mono", Menlo, Courier, monospace;
line-height: 10px;
color: #555;
vertical-align: middle;
background-color: #fcfcfc;
border: solid 1px #ccc;
border-bottom-color: #bbb;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #bbb;
}
.task-list-item {
list-style-type: none;
}
.task-list-item+.task-list-item {
margin-top: 3px;
}
.task-list-item input {
margin: 0 0.35em 0.25em -1.6em;
vertical-align: middle;
}
:checked+.radio-label {
z-index: 1;
position: relative;
border-color: #4078c0;
}
.sourceLine {
display: inline-block;
}
code .kw { color: #000000; }
code .dt { color: #ed6a43; }
code .dv { color: #009999; }
code .bn { color: #009999; }
code .fl { color: #009999; }
code .ch { color: #009999; }
code .st { color: #183691; }
code .co { color: #969896; }
code .ot { color: #0086b3; }
code .al { color: #a61717; }
code .fu { color: #63a35c; }
code .er { color: #a61717; background-color: #e3d2d2; }
code .wa { color: #000000; }
code .cn { color: #008080; }
code .sc { color: #008080; }
code .vs { color: #183691; }
code .ss { color: #183691; }
code .im { color: #000000; }
code .va {color: #008080; }
code .cf { color: #000000; }
code .op { color: #000000; }
code .bu { color: #000000; }
code .ex { color: #000000; }
code .pp { color: #999999; }
code .at { color: #008080; }
code .do { color: #969896; }
code .an { color: #008080; }
code .cv { color: #008080; }
code .in { color: #008080; }
</style>
<style>
body {
  box-sizing: border-box;
  min-width: 200px;
  max-width: 980px;
  margin: 0 auto;
  padding: 45px;
  padding-top: 0px;
}
</style>

</head>

<body>

<h1 id="overview-of-the-functionality-provided-by-the-dynutils-package">Overview of the functionality provided by the dynutils package</h1>
<p>Robrecht Cannoodt &amp; Wouter Saelens</p>
<!-- github markdown using
rmarkdown::render("vignettes/functionality.Rmd", output_format = "github_document")
-->

<h2 id="table-of-contents">Table of contents</h2>
<ul>
<li>Manipulation of lists: <a href="functionality.md#add_class-add-a-class-to-an-object"><code>add_class</code></a>, <a href="functionality.md#extend_with-extend-list-with-more-data"><code>extend_with</code></a></li>
<li>Calculations: <a href="functionality.md#calculate_distance-compute-pairwise-distances-between-two-matrices"><code>calculate_distance</code></a>, <a href="functionality.md#project_to_segments-project-a-set-of-points-to-to-set-of-segments"><code>project_to_segments</code></a>, <a href="functionality.md#calculate_mean-calculate-a-weighted-mean-between-vectors-or-a-list-of-vectors-supports-the-arithmetic-geometric-and-harmonic-mean"><code>calculate_mean</code></a></li>
<li>Manipulation of matrices: <a href="functionality.md#expand_matrix-add-rows-and-columns-to-a-matrix"><code>expand_matrix</code></a></li>
<li>Scaling of matrices and vectors: <a href="functionality.md#scale_uniform-rescale-data-to-have-a-certain-center-and-max-range"><code>scale_uniform</code></a>, <a href="functionality.md#scale_minmax-rescale-data-to-a---range"><code>scale_minmax</code></a>, <a href="functionality.md#scale_quantile-cut-off-outer-quantiles-and-rescale-to-a---range"><code>scale_quantile</code></a></li>
<li>Manipulation of functions: <a href="functionality.md#inherit_default_params-have-one-function-inherit-the-default-parameters-from-other-functions"><code>inherit_default_params</code></a></li>
<li>Manipulation of packages: <a href="functionality.md#check_packages-easily-checking-whether-certain-packages-are-installed"><code>check_packages</code></a>, <a href="functionality.md#install_packages-install-packages-taking-into-account-the-remotes-of-another"><code>install_packages</code></a></li>
<li>Manipulation of vectors: <a href="functionality.md#random_time_string-generates-a-string-very-likely-to-be-unique"><code>random_time_string</code></a></li>
<li>Tibble helpers: <a href="functionality.md#list_as_tibble-convert-a-list-of-lists-to-a-tibble-whilst-retaining-class-information"><code>list_as_tibble</code></a>, <a href="functionality.md#tibble_as_list-convert-a-tibble-back-to-a-list-of-lists-whilst-retaining-class-information"><code>tibble_as_list</code></a>, <a href="functionality.md#extract_row_to_list-extracts-one-row-from-a-tibble-and-converts-it-to-a-list"><code>extract_row_to_list</code></a>, <a href="functionality.md#mapdf-apply-a-function-to-each-row-of-a-data-frame"><code>mapdf</code></a></li>
<li>File helpers: <a href="functionality.md#safe_tempdir-create-an-empty-temporary-directory-and-return-its-path"><code>safe_tempdir</code></a></li>
<li>Assertion helpers: <a href="functionality.md#all_in-check-whether-a-vector-are-all-elements-of-another-vector"><code>%all_in%</code></a>, <a href="functionality.md#has_names-check-whether-an-object-has-certain-names"><code>%has_names%</code></a>, <a href="functionality.md#is_single_numeric-check-whether-a-value-is-a-single-numeric"><code>is_single_numeric</code></a>, <a href="functionality.md#is_bounded-check-whether-a-value-within-a-certain-interval"><code>is_bounded</code></a></li>
</ul>
<h2 id="manipulation-of-lists">Manipulation of lists</h2>
<h3 id="add_class-add-a-class-to-an-object"><code>add_class</code>: Add a class to an object</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1">l &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="dt">important_number =</span> <span class="dv">42</span>) <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">add_class</span>(<span class="st">&quot;my_list&quot;</span>)</a>
<a class="sourceLine" id="cb1-2" data-line-number="2">l</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="co">#&gt; $important_number</span></a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="co">#&gt; [1] 42</span></a>
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb1-6" data-line-number="6"><span class="co">#&gt; attr(,&quot;class&quot;)</span></a>
<a class="sourceLine" id="cb1-7" data-line-number="7"><span class="co">#&gt; [1] &quot;my_list&quot; &quot;list&quot;</span></a></code></pre></div>
<h3 id="extend_with-extend-list-with-more-data"><code>extend_with</code>: Extend list with more data</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1">l <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">extend_with</span>(</a>
<a class="sourceLine" id="cb2-2" data-line-number="2">  <span class="dt">.class_name =</span> <span class="st">&quot;improved_list&quot;</span>, </a>
<a class="sourceLine" id="cb2-3" data-line-number="3">  <span class="dt">url =</span> <span class="st">&quot;https://github.com/dynverse/dynverse&quot;</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4">)</a>
<a class="sourceLine" id="cb2-5" data-line-number="5"><span class="co">#&gt; $important_number</span></a>
<a class="sourceLine" id="cb2-6" data-line-number="6"><span class="co">#&gt; [1] 42</span></a>
<a class="sourceLine" id="cb2-7" data-line-number="7"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb2-8" data-line-number="8"><span class="co">#&gt; $url</span></a>
<a class="sourceLine" id="cb2-9" data-line-number="9"><span class="co">#&gt; [1] &quot;https://github.com/dynverse/dynverse&quot;</span></a>
<a class="sourceLine" id="cb2-10" data-line-number="10"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb2-11" data-line-number="11"><span class="co">#&gt; attr(,&quot;class&quot;)</span></a>
<a class="sourceLine" id="cb2-12" data-line-number="12"><span class="co">#&gt; [1] &quot;improved_list&quot; &quot;my_list&quot;       &quot;list&quot;</span></a></code></pre></div>
<h2 id="calculations">Calculations</h2>
<h3 id="calculate_distance-compute-pairwise-distances-between-two-matrices"><code>calculate_distance</code>: Compute pairwise distances between two matrices</h3>
<p>See <code>?calculate_distance</code> for the list of currently supported distances.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" data-line-number="1">x &lt;-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">runif</span>(<span class="dv">30</span>), <span class="dt">ncol =</span> <span class="dv">10</span>)</a>
<a class="sourceLine" id="cb3-2" data-line-number="2">y &lt;-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">runif</span>(<span class="dv">50</span>), <span class="dt">ncol =</span> <span class="dv">10</span>)</a>
<a class="sourceLine" id="cb3-3" data-line-number="3"><span class="kw">calculate_distance</span>(x, y, <span class="dt">method =</span> <span class="st">&quot;euclidean&quot;</span>)</a>
<a class="sourceLine" id="cb3-4" data-line-number="4"><span class="co">#&gt;          [,1]      [,2]      [,3]     [,4]      [,5]</span></a>
<a class="sourceLine" id="cb3-5" data-line-number="5"><span class="co">#&gt; [1,] 1.184305 0.9571034 1.1284052 1.022205 1.5531565</span></a>
<a class="sourceLine" id="cb3-6" data-line-number="6"><span class="co">#&gt; [2,] 1.078353 1.1378581 0.7076092 1.072164 0.8846294</span></a>
<a class="sourceLine" id="cb3-7" data-line-number="7"><span class="co">#&gt; [3,] 1.335627 1.4116406 1.3526085 1.239853 1.6282805</span></a></code></pre></div>
<p>For euclidean distances, this is similar to calculating:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="kw">as.matrix</span>(<span class="kw">dist</span>(<span class="kw">rbind</span>(x, y)))[<span class="dv">1</span><span class="op">:</span><span class="dv">3</span>, <span class="dv">-1</span><span class="op">:-</span><span class="dv">3</span>]</a>
<a class="sourceLine" id="cb4-2" data-line-number="2"><span class="co">#&gt;          4         5         6        7         8</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3"><span class="co">#&gt; 1 1.184305 0.9571034 1.1284052 1.022205 1.5531565</span></a>
<a class="sourceLine" id="cb4-4" data-line-number="4"><span class="co">#&gt; 2 1.078353 1.1378581 0.7076092 1.072164 0.8846294</span></a>
<a class="sourceLine" id="cb4-5" data-line-number="5"><span class="co">#&gt; 3 1.335627 1.4116406 1.3526085 1.239853 1.6282805</span></a></code></pre></div>
<h3 id="project_to_segments-project-a-set-of-points-to-to-set-of-segments"><code>project_to_segments</code>: Project a set of points to to set of segments</h3>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb5-1" data-line-number="1">x &lt;-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">rnorm</span>(<span class="dv">50</span>, <span class="dv">0</span>, <span class="fl">.5</span>), <span class="dt">ncol =</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">segfrom &lt;-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">0</span>, <span class="dv">-1</span>, <span class="dv">1</span>, <span class="dv">0</span>, <span class="dv">-1</span>, <span class="dv">0</span>), <span class="dt">ncol =</span> <span class="dv">2</span>, <span class="dt">byrow =</span> <span class="ot">TRUE</span>)</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">segto &lt;-<span class="st"> </span>segfrom <span class="op">/</span><span class="st"> </span><span class="dv">10</span></a>
<a class="sourceLine" id="cb5-4" data-line-number="4">fit &lt;-<span class="st"> </span><span class="kw">project_to_segments</span>(x, segfrom, segto)</a>
<a class="sourceLine" id="cb5-5" data-line-number="5"></a>
<a class="sourceLine" id="cb5-6" data-line-number="6"><span class="kw">ggplot</span>() <span class="op">+</span></a>
<a class="sourceLine" id="cb5-7" data-line-number="7"><span class="st">  </span><span class="kw">geom_segment</span>(<span class="kw">aes</span>(<span class="dt">x =</span> x[,<span class="dv">1</span>], <span class="dt">xend =</span> fit<span class="op">$</span>x_proj[,<span class="dv">1</span>], <span class="dt">y =</span> x[,<span class="dv">2</span>], <span class="dt">yend =</span> fit<span class="op">$</span>x_proj[,<span class="dv">2</span>], <span class="dt">colour =</span> <span class="st">&quot;projection&quot;</span>), <span class="dt">linetype =</span> <span class="st">&quot;dashed&quot;</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb5-8" data-line-number="8"><span class="st">  </span><span class="kw">geom_point</span>(<span class="kw">aes</span>(x[,<span class="dv">1</span>], x[,<span class="dv">2</span>], <span class="dt">colour =</span> <span class="st">&quot;point&quot;</span>)) <span class="op">+</span></a>
<a class="sourceLine" id="cb5-9" data-line-number="9"><span class="st">  </span><span class="kw">geom_segment</span>(<span class="kw">aes</span>(<span class="dt">x =</span> segfrom[,<span class="dv">1</span>], <span class="dt">xend =</span> segto[,<span class="dv">1</span>], <span class="dt">y =</span> segfrom[,<span class="dv">2</span>], <span class="dt">yend =</span> segto[,<span class="dv">2</span>], <span class="dt">colour =</span> <span class="st">&quot;segment&quot;</span>)) <span class="op">+</span></a>
<a class="sourceLine" id="cb5-10" data-line-number="10"><span class="st">  </span><span class="kw">scale_colour_brewer</span>(<span class="dt">palette =</span> <span class="st">&quot;Dark2&quot;</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb5-11" data-line-number="11"><span class="st">  </span><span class="kw">scale_x_continuous</span>(<span class="dt">name =</span> <span class="ot">NULL</span>, <span class="dt">breaks =</span> <span class="ot">NULL</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb5-12" data-line-number="12"><span class="st">  </span><span class="kw">scale_y_continuous</span>(<span class="dt">name =</span> <span class="ot">NULL</span>, <span class="dt">breaks =</span> <span class="ot">NULL</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb5-13" data-line-number="13"><span class="st">  </span><span class="kw">labs</span>(<span class="dt">colour =</span> <span class="st">&quot;Object type&quot;</span>) <span class="op">+</span></a>
<a class="sourceLine" id="cb5-14" data-line-number="14"><span class="st">  </span><span class="kw">theme_classic</span>()</a></code></pre></div>
<p><img src="" /><!-- --></p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb6-1" data-line-number="1"></a>
<a class="sourceLine" id="cb6-2" data-line-number="2"><span class="kw">str</span>(fit)</a>
<a class="sourceLine" id="cb6-3" data-line-number="3"><span class="co">#&gt; List of 4</span></a>
<a class="sourceLine" id="cb6-4" data-line-number="4"><span class="co">#&gt;  $ x_proj     : num [1:25, 1:2] 0 0 0 0.278 0 ...</span></a>
<a class="sourceLine" id="cb6-5" data-line-number="5"><span class="co">#&gt;  $ distance   : num [1:25] 0.0068 0.01605 0.12144 0.00587 0.12604 ...</span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6"><span class="co">#&gt;  $ segment    : int [1:25] 1 2 1 3 1 4 2 3 2 2 ...</span></a>
<a class="sourceLine" id="cb6-7" data-line-number="7"><span class="co">#&gt;  $ progression: num [1:25] 1 0.108 0.297 0.802 0 ...</span></a></code></pre></div>
<h3 id="calculate_mean-calculate-a-weighted-mean-between-vectors-or-a-list-of-vectors-supports-the-arithmetic-geometric-and-harmonic-mean"><code>calculate_mean</code>: Calculate a (weighted) mean between vectors or a list of vectors; supports the arithmetic, geometric and harmonic mean</h3>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="kw">calculate_arithmetic_mean</span>(<span class="fl">0.1</span>, <span class="fl">0.5</span>, <span class="fl">0.9</span>)</a>
<a class="sourceLine" id="cb7-2" data-line-number="2"><span class="co">#&gt; [1] 0.5</span></a>
<a class="sourceLine" id="cb7-3" data-line-number="3"><span class="kw">calculate_geometric_mean</span>(<span class="fl">0.1</span>, <span class="fl">0.5</span>, <span class="fl">0.9</span>)</a>
<a class="sourceLine" id="cb7-4" data-line-number="4"><span class="co">#&gt; [1] 0.3556893</span></a>
<a class="sourceLine" id="cb7-5" data-line-number="5"><span class="kw">calculate_harmonic_mean</span>(<span class="fl">0.1</span>, <span class="fl">0.5</span>, <span class="fl">0.9</span>)</a>
<a class="sourceLine" id="cb7-6" data-line-number="6"><span class="co">#&gt; [1] 0.2288136</span></a>
<a class="sourceLine" id="cb7-7" data-line-number="7"><span class="kw">calculate_mean</span>(.<span class="dv">1</span>, <span class="fl">.5</span>, <span class="fl">.9</span>, <span class="dt">method =</span> <span class="st">&quot;harmonic&quot;</span>)</a>
<a class="sourceLine" id="cb7-8" data-line-number="8"><span class="co">#&gt; [1] 0.2288136</span></a>
<a class="sourceLine" id="cb7-9" data-line-number="9"></a>
<a class="sourceLine" id="cb7-10" data-line-number="10"><span class="co"># example with multiple vectors</span></a>
<a class="sourceLine" id="cb7-11" data-line-number="11"><span class="kw">calculate_arithmetic_mean</span>(<span class="kw">c</span>(<span class="fl">0.1</span>, <span class="fl">0.9</span>), <span class="kw">c</span>(<span class="fl">0.2</span>, <span class="dv">1</span>))</a>
<a class="sourceLine" id="cb7-12" data-line-number="12"><span class="co">#&gt; [1] 0.15 0.95</span></a>
<a class="sourceLine" id="cb7-13" data-line-number="13"></a>
<a class="sourceLine" id="cb7-14" data-line-number="14"><span class="co"># example with a list of vectors</span></a>
<a class="sourceLine" id="cb7-15" data-line-number="15">vectors &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="kw">c</span>(<span class="fl">0.1</span>, <span class="fl">0.2</span>), <span class="kw">c</span>(<span class="fl">0.4</span>, <span class="fl">0.5</span>))</a>
<a class="sourceLine" id="cb7-16" data-line-number="16"><span class="kw">calculate_geometric_mean</span>(vectors)</a>
<a class="sourceLine" id="cb7-17" data-line-number="17"><span class="co">#&gt; [1] 0.2000000 0.3162278</span></a>
<a class="sourceLine" id="cb7-18" data-line-number="18"></a>
<a class="sourceLine" id="cb7-19" data-line-number="19"><span class="co"># example of weighted means</span></a>
<a class="sourceLine" id="cb7-20" data-line-number="20"><span class="kw">calculate_geometric_mean</span>(<span class="kw">c</span>(<span class="fl">0.1</span>, <span class="dv">10</span>), <span class="kw">c</span>(<span class="fl">0.9</span>, <span class="dv">20</span>), <span class="kw">c</span>(<span class="fl">0.5</span>, <span class="dv">2</span>), <span class="dt">weights =</span> <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">5</span>))</a>
<a class="sourceLine" id="cb7-21" data-line-number="21"><span class="co">#&gt; [1] 0.4736057 4.3491186</span></a></code></pre></div>
<h2 id="manipulation-of-matrices">Manipulation of matrices</h2>
<h3 id="expand_matrix-add-rows-and-columns-to-a-matrix"><code>expand_matrix</code>: Add rows and columns to a matrix</h3>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb8-1" data-line-number="1">x &lt;-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">runif</span>(<span class="dv">12</span>), <span class="dt">ncol =</span> <span class="dv">4</span>, <span class="dt">dimnames =</span> <span class="kw">list</span>(<span class="kw">c</span>(<span class="st">&quot;a&quot;</span>, <span class="st">&quot;c&quot;</span>, <span class="st">&quot;d&quot;</span>), <span class="kw">c</span>(<span class="st">&quot;D&quot;</span>, <span class="st">&quot;F&quot;</span>, <span class="st">&quot;H&quot;</span>, <span class="st">&quot;I&quot;</span>)))</a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="kw">expand_matrix</span>(x, letters[<span class="dv">1</span><span class="op">:</span><span class="dv">5</span>], LETTERS[<span class="dv">1</span><span class="op">:</span><span class="dv">10</span>], <span class="dt">fill =</span> <span class="dv">0</span>)</a>
<a class="sourceLine" id="cb8-3" data-line-number="3"><span class="co">#&gt;   A B C         D E         F G         H         I J</span></a>
<a class="sourceLine" id="cb8-4" data-line-number="4"><span class="co">#&gt; a 0 0 0 0.2937302 0 0.5033395 0 0.7581031 0.5476466 0</span></a>
<a class="sourceLine" id="cb8-5" data-line-number="5"><span class="co">#&gt; b 0 0 0 0.0000000 0 0.0000000 0 0.0000000 0.0000000 0</span></a>
<a class="sourceLine" id="cb8-6" data-line-number="6"><span class="co">#&gt; c 0 0 0 0.1912601 0 0.8770575 0 0.7244989 0.7117439 0</span></a>
<a class="sourceLine" id="cb8-7" data-line-number="7"><span class="co">#&gt; d 0 0 0 0.8864509 0 0.1891936 0 0.9437248 0.3889051 0</span></a>
<a class="sourceLine" id="cb8-8" data-line-number="8"><span class="co">#&gt; e 0 0 0 0.0000000 0 0.0000000 0 0.0000000 0.0000000 0</span></a></code></pre></div>
<h2 id="scaling-of-matrices-and-vectors">Scaling of matrices and vectors</h2>
<h3 id="scale_uniform-rescale-data-to-have-a-certain-center-and-max-range"><code>scale_uniform</code>: Rescale data to have a certain center and max range</h3>
<p>Generate a matrix from a normal distribution with a large standard deviation, centered at c(5, 5).</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb9-1" data-line-number="1">x &lt;-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">rnorm</span>(<span class="dv">200</span><span class="op">*</span><span class="dv">2</span>, <span class="dt">sd =</span> <span class="dv">10</span>, <span class="dt">mean =</span> <span class="dv">5</span>), <span class="dt">ncol =</span> <span class="dv">2</span>)</a></code></pre></div>
<p>Center the dataset at c(0, 0) with a minimum of c(-.5, -.5) and a maximum of c(.5, .5).</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb10-1" data-line-number="1">x_scaled &lt;-<span class="st"> </span><span class="kw">scale_uniform</span>(x, <span class="dt">center =</span> <span class="dv">0</span>, <span class="dt">max_range =</span> <span class="dv">1</span>)</a></code></pre></div>
<p>Check the ranges and verify that the scaling is correct.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb11-1" data-line-number="1">ranges &lt;-<span class="st"> </span><span class="kw">apply</span>(x_scaled, <span class="dv">2</span>, range)</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">ranges                   <span class="co"># should all lie between -.5 and .5</span></a>
<a class="sourceLine" id="cb11-3" data-line-number="3"><span class="co">#&gt;            [,1] [,2]</span></a>
<a class="sourceLine" id="cb11-4" data-line-number="4"><span class="co">#&gt; [1,] -0.4061179 -0.5</span></a>
<a class="sourceLine" id="cb11-5" data-line-number="5"><span class="co">#&gt; [2,]  0.4061179  0.5</span></a>
<a class="sourceLine" id="cb11-6" data-line-number="6"><span class="kw">colMeans</span>(ranges)         <span class="co"># should all be equal to 0</span></a>
<a class="sourceLine" id="cb11-7" data-line-number="7"><span class="co">#&gt; [1] 0 0</span></a>
<a class="sourceLine" id="cb11-8" data-line-number="8"><span class="kw">apply</span>(ranges, <span class="dv">2</span>, diff)   <span class="co"># max should be 1</span></a>
<a class="sourceLine" id="cb11-9" data-line-number="9"><span class="co">#&gt; [1] 0.8122358 1.0000000</span></a></code></pre></div>
<h3 id="scale_minmax-rescale-data-to-a-0-1-range"><code>scale_minmax</code>: Rescale data to a [0, 1] range</h3>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb12-1" data-line-number="1">x_scaled2 &lt;-<span class="st"> </span><span class="kw">scale_minmax</span>(x)</a></code></pre></div>
<p>Check the ranges and verify that the scaling is correct.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="kw">apply</span>(x_scaled2, <span class="dv">2</span>, range)  <span class="co"># each column should be [0, 1]</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2"><span class="co">#&gt;      [,1] [,2]</span></a>
<a class="sourceLine" id="cb13-3" data-line-number="3"><span class="co">#&gt; [1,]    0    0</span></a>
<a class="sourceLine" id="cb13-4" data-line-number="4"><span class="co">#&gt; [2,]    1    1</span></a></code></pre></div>
<h3 id="scale_quantile-cut-off-outer-quantiles-and-rescale-to-a-0-1-range"><code>scale_quantile</code>: Cut off outer quantiles and rescale to a [0, 1] range</h3>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb14-1" data-line-number="1">x_scaled3 &lt;-<span class="st"> </span><span class="kw">scale_quantile</span>(x, <span class="fl">.05</span>)</a></code></pre></div>
<p>Check the ranges and verify that the scaling is correct.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb15-1" data-line-number="1"><span class="kw">apply</span>(x_scaled3, <span class="dv">2</span>, range)   <span class="co"># each column should be [0, 1]</span></a>
<a class="sourceLine" id="cb15-2" data-line-number="2"><span class="co">#&gt;      [,1] [,2]</span></a>
<a class="sourceLine" id="cb15-3" data-line-number="3"><span class="co">#&gt; [1,]    0    0</span></a>
<a class="sourceLine" id="cb15-4" data-line-number="4"><span class="co">#&gt; [2,]    1    1</span></a>
<a class="sourceLine" id="cb15-5" data-line-number="5"><span class="kw">qplot</span>(x_scaled2[,<span class="dv">1</span>], x_scaled3[,<span class="dv">1</span>]) <span class="op">+</span><span class="st"> </span><span class="kw">theme_bw</span>()</a></code></pre></div>
<p><img src="" /><!-- --></p>
<h2 id="manipulation-of-functions">Manipulation of functions</h2>
<h3 id="inherit_default_params-have-one-function-inherit-the-default-parameters-from-other-functions"><code>inherit_default_params</code>: Have one function inherit the default parameters from other functions</h3>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb16-1" data-line-number="1">fun1 &lt;-<span class="st"> </span><span class="cf">function</span>(<span class="dt">a =</span> <span class="dv">10</span>, <span class="dt">b =</span> <span class="dv">7</span>) <span class="kw">runif</span>(a, <span class="op">-</span>b, b)</a>
<a class="sourceLine" id="cb16-2" data-line-number="2">fun2 &lt;-<span class="st"> </span><span class="cf">function</span>(<span class="dt">c =</span> <span class="dv">9</span>) <span class="dv">2</span><span class="op">^</span>c</a>
<a class="sourceLine" id="cb16-3" data-line-number="3"></a>
<a class="sourceLine" id="cb16-4" data-line-number="4">fun3 &lt;-<span class="st"> </span><span class="kw">inherit_default_params</span>(</a>
<a class="sourceLine" id="cb16-5" data-line-number="5">  <span class="dt">super =</span> <span class="kw">list</span>(fun1, fun2),</a>
<a class="sourceLine" id="cb16-6" data-line-number="6">  <span class="dt">fun =</span> <span class="cf">function</span>(a, b, c) {</a>
<a class="sourceLine" id="cb16-7" data-line-number="7">    <span class="kw">list</span>(<span class="dt">x =</span> <span class="kw">fun1</span>(a, b), <span class="dt">y =</span> <span class="kw">fun2</span>(c))</a>
<a class="sourceLine" id="cb16-8" data-line-number="8">  }</a>
<a class="sourceLine" id="cb16-9" data-line-number="9">)</a>
<a class="sourceLine" id="cb16-10" data-line-number="10"></a>
<a class="sourceLine" id="cb16-11" data-line-number="11">fun3</a>
<a class="sourceLine" id="cb16-12" data-line-number="12"><span class="co">#&gt; function (a = 10, b = 7, c = 9) </span></a>
<a class="sourceLine" id="cb16-13" data-line-number="13"><span class="co">#&gt; {</span></a>
<a class="sourceLine" id="cb16-14" data-line-number="14"><span class="co">#&gt;     list(x = fun1(a, b), y = fun2(c))</span></a>
<a class="sourceLine" id="cb16-15" data-line-number="15"><span class="co">#&gt; }</span></a></code></pre></div>
<h2 id="manipulation-of-packages">Manipulation of packages</h2>
<h3 id="check_packages-easily-checking-whether-certain-packages-are-installed"><code>check_packages</code>: Easily checking whether certain packages are installed</h3>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="kw">check_packages</span>(<span class="st">&quot;SCORPIUS&quot;</span>, <span class="st">&quot;dynutils&quot;</span>, <span class="st">&quot;wubbalubbadubdub&quot;</span>)</a>
<a class="sourceLine" id="cb17-2" data-line-number="2"><span class="co">#&gt;         SCORPIUS         dynutils wubbalubbadubdub </span></a>
<a class="sourceLine" id="cb17-3" data-line-number="3"><span class="co">#&gt;             TRUE             TRUE            FALSE</span></a>
<a class="sourceLine" id="cb17-4" data-line-number="4"><span class="kw">check_packages</span>(<span class="kw">c</span>(<span class="st">&quot;princurve&quot;</span>, <span class="st">&quot;mlr&quot;</span>, <span class="st">&quot;tidyverse&quot;</span>))</a>
<a class="sourceLine" id="cb17-5" data-line-number="5"><span class="co">#&gt; princurve       mlr tidyverse </span></a>
<a class="sourceLine" id="cb17-6" data-line-number="6"><span class="co">#&gt;      TRUE      TRUE      TRUE</span></a></code></pre></div>
<h3 id="install_packages-install-packages-taking-into-account-the-remotes-of-another"><code>install_packages</code>: Install packages taking into account the remotes of another</h3>
<p>This is useful for installing suggested packages with GitHub remotes.</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="kw">install_packages</span>(<span class="st">&quot;SCORPIUS&quot;</span>, <span class="dt">package =</span> <span class="st">&quot;dynmethods&quot;</span>, <span class="dt">prompt =</span> <span class="ot">TRUE</span>)</a></code></pre></div>
<pre><code>&gt; install_packages(&quot;SCORPIUS&quot;, package = &quot;dynmethods&quot;, prompt = TRUE)
Following packages have to be installed: SCORPIUS
Do you want to install these packages? (y/yes/1 or n/no/2): 1
Installing SCORPIUS
...
** testing if installed package can be loaded
* DONE (SCORPIUS)
Installed SCORPIUS
[1] &quot;SCORPIUS&quot;
</code></pre>
<h2 id="manipulation-of-vectors">Manipulation of vectors</h2>
<h3 id="random_time_string-generates-a-string-very-likely-to-be-unique"><code>random_time_string</code>: Generates a string very likely to be unique</h3>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="kw">random_time_string</span>(<span class="st">&quot;test&quot;</span>)</a>
<a class="sourceLine" id="cb20-2" data-line-number="2"><span class="co">#&gt; [1] &quot;20190705_115004__test__syYeN626jT&quot;</span></a>
<a class="sourceLine" id="cb20-3" data-line-number="3"></a>
<a class="sourceLine" id="cb20-4" data-line-number="4"><span class="kw">random_time_string</span>(<span class="st">&quot;test&quot;</span>)</a>
<a class="sourceLine" id="cb20-5" data-line-number="5"><span class="co">#&gt; [1] &quot;20190705_115004__test__e6MCnXcJM7&quot;</span></a>
<a class="sourceLine" id="cb20-6" data-line-number="6"></a>
<a class="sourceLine" id="cb20-7" data-line-number="7"><span class="kw">random_time_string</span>(<span class="st">&quot;test&quot;</span>)</a>
<a class="sourceLine" id="cb20-8" data-line-number="8"><span class="co">#&gt; [1] &quot;20190705_115004__test__9pFJ5mxS3R&quot;</span></a></code></pre></div>
<h2 id="tibble-helpers">Tibble helpers</h2>
<h3 id="list_as_tibble-convert-a-list-of-lists-to-a-tibble-whilst-retaining-class-information"><code>list_as_tibble</code>: Convert a list of lists to a tibble whilst retaining class information</h3>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb21-1" data-line-number="1">li &lt;-<span class="st"> </span><span class="kw">list</span>(</a>
<a class="sourceLine" id="cb21-2" data-line-number="2">  <span class="kw">list</span>(<span class="dt">a =</span> <span class="dv">1</span>, <span class="dt">b =</span> log10, <span class="dt">c =</span> <span class="st">&quot;parrot&quot;</span>) <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">add_class</span>(<span class="st">&quot;myobject&quot;</span>), </a>
<a class="sourceLine" id="cb21-3" data-line-number="3">  <span class="kw">list</span>(<span class="dt">a =</span> <span class="dv">2</span>, <span class="dt">b =</span> sqrt, <span class="dt">c =</span> <span class="st">&quot;quest&quot;</span>) <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">add_class</span>(<span class="st">&quot;yourobject&quot;</span>)</a>
<a class="sourceLine" id="cb21-4" data-line-number="4">)</a>
<a class="sourceLine" id="cb21-5" data-line-number="5"></a>
<a class="sourceLine" id="cb21-6" data-line-number="6">tib &lt;-<span class="st"> </span><span class="kw">list_as_tibble</span>(li)</a>
<a class="sourceLine" id="cb21-7" data-line-number="7"></a>
<a class="sourceLine" id="cb21-8" data-line-number="8">tib</a>
<a class="sourceLine" id="cb21-9" data-line-number="9"><span class="co">#&gt; # A tibble: 2 x 4</span></a>
<a class="sourceLine" id="cb21-10" data-line-number="10"><span class="co">#&gt;       a b      c      .object_class</span></a>
<a class="sourceLine" id="cb21-11" data-line-number="11"><span class="co">#&gt;   &lt;dbl&gt; &lt;list&gt; &lt;chr&gt;  &lt;list&gt;       </span></a>
<a class="sourceLine" id="cb21-12" data-line-number="12"><span class="co">#&gt; 1     1 &lt;fn&gt;   parrot &lt;chr [2]&gt;    </span></a>
<a class="sourceLine" id="cb21-13" data-line-number="13"><span class="co">#&gt; 2     2 &lt;fn&gt;   quest  &lt;chr [2]&gt;</span></a></code></pre></div>
<h3 id="tibble_as_list-convert-a-tibble-back-to-a-list-of-lists-whilst-retaining-class-information"><code>tibble_as_list</code>: Convert a tibble back to a list of lists whilst retaining class information</h3>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb22-1" data-line-number="1">li &lt;-<span class="st"> </span><span class="kw">tibble_as_list</span>(tib)</a>
<a class="sourceLine" id="cb22-2" data-line-number="2"></a>
<a class="sourceLine" id="cb22-3" data-line-number="3">li</a>
<a class="sourceLine" id="cb22-4" data-line-number="4"><span class="co">#&gt; [[1]]</span></a>
<a class="sourceLine" id="cb22-5" data-line-number="5"><span class="co">#&gt; $a</span></a>
<a class="sourceLine" id="cb22-6" data-line-number="6"><span class="co">#&gt; [1] 1</span></a>
<a class="sourceLine" id="cb22-7" data-line-number="7"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb22-8" data-line-number="8"><span class="co">#&gt; $b</span></a>
<a class="sourceLine" id="cb22-9" data-line-number="9"><span class="co">#&gt; function (x)  .Primitive(&quot;log10&quot;)</span></a>
<a class="sourceLine" id="cb22-10" data-line-number="10"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb22-11" data-line-number="11"><span class="co">#&gt; $c</span></a>
<a class="sourceLine" id="cb22-12" data-line-number="12"><span class="co">#&gt; [1] &quot;parrot&quot;</span></a>
<a class="sourceLine" id="cb22-13" data-line-number="13"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb22-14" data-line-number="14"><span class="co">#&gt; attr(,&quot;class&quot;)</span></a>
<a class="sourceLine" id="cb22-15" data-line-number="15"><span class="co">#&gt; [1] &quot;myobject&quot; &quot;list&quot;    </span></a>
<a class="sourceLine" id="cb22-16" data-line-number="16"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb22-17" data-line-number="17"><span class="co">#&gt; [[2]]</span></a>
<a class="sourceLine" id="cb22-18" data-line-number="18"><span class="co">#&gt; $a</span></a>
<a class="sourceLine" id="cb22-19" data-line-number="19"><span class="co">#&gt; [1] 2</span></a>
<a class="sourceLine" id="cb22-20" data-line-number="20"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb22-21" data-line-number="21"><span class="co">#&gt; $b</span></a>
<a class="sourceLine" id="cb22-22" data-line-number="22"><span class="co">#&gt; function (x)  .Primitive(&quot;sqrt&quot;)</span></a>
<a class="sourceLine" id="cb22-23" data-line-number="23"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb22-24" data-line-number="24"><span class="co">#&gt; $c</span></a>
<a class="sourceLine" id="cb22-25" data-line-number="25"><span class="co">#&gt; [1] &quot;quest&quot;</span></a>
<a class="sourceLine" id="cb22-26" data-line-number="26"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb22-27" data-line-number="27"><span class="co">#&gt; attr(,&quot;class&quot;)</span></a>
<a class="sourceLine" id="cb22-28" data-line-number="28"><span class="co">#&gt; [1] &quot;yourobject&quot; &quot;list&quot;</span></a></code></pre></div>
<h3 id="extract_row_to_list-extracts-one-row-from-a-tibble-and-converts-it-to-a-list"><code>extract_row_to_list</code>: Extracts one row from a tibble and converts it to a list</h3>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb23-1" data-line-number="1"><span class="kw">extract_row_to_list</span>(tib, <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb23-2" data-line-number="2"><span class="co">#&gt; $a</span></a>
<a class="sourceLine" id="cb23-3" data-line-number="3"><span class="co">#&gt; [1] 2</span></a>
<a class="sourceLine" id="cb23-4" data-line-number="4"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb23-5" data-line-number="5"><span class="co">#&gt; $b</span></a>
<a class="sourceLine" id="cb23-6" data-line-number="6"><span class="co">#&gt; function (x)  .Primitive(&quot;sqrt&quot;)</span></a>
<a class="sourceLine" id="cb23-7" data-line-number="7"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb23-8" data-line-number="8"><span class="co">#&gt; $c</span></a>
<a class="sourceLine" id="cb23-9" data-line-number="9"><span class="co">#&gt; [1] &quot;quest&quot;</span></a>
<a class="sourceLine" id="cb23-10" data-line-number="10"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb23-11" data-line-number="11"><span class="co">#&gt; attr(,&quot;class&quot;)</span></a>
<a class="sourceLine" id="cb23-12" data-line-number="12"><span class="co">#&gt; [1] &quot;yourobject&quot; &quot;list&quot;</span></a></code></pre></div>
<h3 id="mapdf-apply-a-function-to-each-row-of-a-data-frame"><code>mapdf</code>: Apply a function to each row of a data frame</h3>
<p>The <code>mapdf</code> functions apply a function on each row of a data frame. They are based heavily on purrr’s <code>map</code> functions.</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb24-1" data-line-number="1">tib <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">mapdf</span>(class)</a>
<a class="sourceLine" id="cb24-2" data-line-number="2"><span class="co">#&gt; [[1]]</span></a>
<a class="sourceLine" id="cb24-3" data-line-number="3"><span class="co">#&gt; [1] &quot;myobject&quot; &quot;list&quot;    </span></a>
<a class="sourceLine" id="cb24-4" data-line-number="4"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb24-5" data-line-number="5"><span class="co">#&gt; [[2]]</span></a>
<a class="sourceLine" id="cb24-6" data-line-number="6"><span class="co">#&gt; [1] &quot;yourobject&quot; &quot;list&quot;</span></a></code></pre></div>
<p>Or use an anonymous function.</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb25-1" data-line-number="1">tib <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">mapdf</span>(<span class="cf">function</span>(row) <span class="kw">paste0</span>(row<span class="op">$</span><span class="kw">b</span>(row<span class="op">$</span>a), <span class="st">&quot;_&quot;</span>, row<span class="op">$</span>c))</a>
<a class="sourceLine" id="cb25-2" data-line-number="2"><span class="co">#&gt; [[1]]</span></a>
<a class="sourceLine" id="cb25-3" data-line-number="3"><span class="co">#&gt; [1] &quot;0_parrot&quot;</span></a>
<a class="sourceLine" id="cb25-4" data-line-number="4"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb25-5" data-line-number="5"><span class="co">#&gt; [[2]]</span></a>
<a class="sourceLine" id="cb25-6" data-line-number="6"><span class="co">#&gt; [1] &quot;1.4142135623731_quest&quot;</span></a></code></pre></div>
<p>Or even a formula.</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb26-1" data-line-number="1">tib <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">mapdf</span>(<span class="op">~</span><span class="st"> </span>.<span class="op">$</span>b)</a>
<a class="sourceLine" id="cb26-2" data-line-number="2"><span class="co">#&gt; [[1]]</span></a>
<a class="sourceLine" id="cb26-3" data-line-number="3"><span class="co">#&gt; function (x)  .Primitive(&quot;log10&quot;)</span></a>
<a class="sourceLine" id="cb26-4" data-line-number="4"><span class="co">#&gt; </span></a>
<a class="sourceLine" id="cb26-5" data-line-number="5"><span class="co">#&gt; [[2]]</span></a>
<a class="sourceLine" id="cb26-6" data-line-number="6"><span class="co">#&gt; function (x)  .Primitive(&quot;sqrt&quot;)</span></a></code></pre></div>
<p>There are many more variations available. See <code>?mapdf</code> for more info.</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb27-1" data-line-number="1">tib <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">mapdf_lgl</span>(<span class="op">~</span><span class="st"> </span>.<span class="op">$</span>a <span class="op">&gt;</span><span class="st"> </span><span class="dv">1</span>)</a>
<a class="sourceLine" id="cb27-2" data-line-number="2"><span class="co">#&gt; [1] FALSE  TRUE</span></a>
<a class="sourceLine" id="cb27-3" data-line-number="3">tib <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">mapdf_chr</span>(<span class="op">~</span><span class="st"> </span><span class="kw">paste0</span>(<span class="st">&quot;~&quot;</span>, .<span class="op">$</span>c, <span class="st">&quot;~&quot;</span>))</a>
<a class="sourceLine" id="cb27-4" data-line-number="4"><span class="co">#&gt; [1] &quot;~parrot~&quot; &quot;~quest~&quot;</span></a>
<a class="sourceLine" id="cb27-5" data-line-number="5">tib <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">mapdf_int</span>(<span class="op">~</span><span class="st"> </span><span class="kw">nchar</span>(.<span class="op">$</span>c))</a>
<a class="sourceLine" id="cb27-6" data-line-number="6"><span class="co">#&gt; [1] 6 5</span></a>
<a class="sourceLine" id="cb27-7" data-line-number="7">tib <span class="op">%&gt;%</span><span class="st"> </span><span class="kw">mapdf_dbl</span>(<span class="op">~</span><span class="st"> </span>.<span class="op">$</span>a <span class="op">*</span><span class="st"> </span><span class="fl">1.234</span>)</a>
<a class="sourceLine" id="cb27-8" data-line-number="8"><span class="co">#&gt; [1] 1.234 2.468</span></a></code></pre></div>
<h2 id="file-helpers">File helpers</h2>
<h3 id="safe_tempdir-create-an-empty-temporary-directory-and-return-its-path"><code>safe_tempdir</code>: Create an empty temporary directory and return its path</h3>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb28-1" data-line-number="1"><span class="kw">safe_tempdir</span>(<span class="st">&quot;samson&quot;</span>)</a>
<a class="sourceLine" id="cb28-2" data-line-number="2"><span class="co">#&gt; [1] &quot;/tmp/RtmprpNY2K/file342b3ade3824/samson&quot;</span></a></code></pre></div>
<h2 id="assertion-helpers">Assertion helpers</h2>
<h3 id="all_in-check-whether-a-vector-are-all-elements-of-another-vector"><code>%all_in%</code>: Check whether a vector are all elements of another vector</h3>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb29-1" data-line-number="1"><span class="kw">library</span>(assertthat)</a>
<a class="sourceLine" id="cb29-2" data-line-number="2"><span class="kw">assert_that</span>(<span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>) <span class="op">%all_in%</span><span class="st"> </span><span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>))</a>
<a class="sourceLine" id="cb29-3" data-line-number="3"><span class="co">#&gt; [1] TRUE</span></a>
<a class="sourceLine" id="cb29-4" data-line-number="4"><span class="kw">assert_that</span>(<span class="st">&quot;a&quot;</span> <span class="op">%all_in%</span><span class="st"> </span>letters)</a>
<a class="sourceLine" id="cb29-5" data-line-number="5"><span class="co">#&gt; [1] TRUE</span></a>
<a class="sourceLine" id="cb29-6" data-line-number="6"><span class="kw">assert_that</span>(<span class="st">&quot;A&quot;</span> <span class="op">%all_in%</span><span class="st"> </span>letters)</a>
<a class="sourceLine" id="cb29-7" data-line-number="7"><span class="co">#&gt; Error: &quot;A&quot; is missing 1 element from letters: &quot;A&quot;</span></a>
<a class="sourceLine" id="cb29-8" data-line-number="8"><span class="kw">assert_that</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">10</span> <span class="op">%all_in%</span><span class="st"> </span>letters)</a>
<a class="sourceLine" id="cb29-9" data-line-number="9"><span class="co">#&gt; Error: 1:10 is missing 10 elements from letters: 1L, 2L, 3L, ...</span></a></code></pre></div>
<h3 id="has_names-check-whether-an-object-has-certain-names"><code>%has_names%</code>: Check whether an object has certain names</h3>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb30-1" data-line-number="1"><span class="kw">assert_that</span>(li <span class="op">%has_names%</span><span class="st"> &quot;a&quot;</span>)</a>
<a class="sourceLine" id="cb30-2" data-line-number="2"><span class="co">#&gt; Error: li is missing 1 name from &quot;a&quot;: &quot;a&quot;</span></a>
<a class="sourceLine" id="cb30-3" data-line-number="3"><span class="kw">assert_that</span>(li <span class="op">%has_names%</span><span class="st"> &quot;c&quot;</span>)</a>
<a class="sourceLine" id="cb30-4" data-line-number="4"><span class="co">#&gt; Error: li is missing 1 name from &quot;c&quot;: &quot;c&quot;</span></a>
<a class="sourceLine" id="cb30-5" data-line-number="5"><span class="kw">assert_that</span>(li <span class="op">%has_names%</span><span class="st"> </span>letters)</a>
<a class="sourceLine" id="cb30-6" data-line-number="6"><span class="co">#&gt; Error: li is missing 26 names from letters: &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, ...</span></a></code></pre></div>
<h3 id="is_single_numeric-check-whether-a-value-is-a-single-numeric"><code>is_single_numeric</code>: Check whether a value is a single numeric</h3>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb31-1" data-line-number="1"><span class="kw">assert_that</span>(<span class="kw">is_single_numeric</span>(<span class="dv">1</span>))</a>
<a class="sourceLine" id="cb31-2" data-line-number="2"><span class="co">#&gt; [1] TRUE</span></a>
<a class="sourceLine" id="cb31-3" data-line-number="3"><span class="kw">assert_that</span>(<span class="kw">is_single_numeric</span>(<span class="ot">Inf</span>))</a>
<a class="sourceLine" id="cb31-4" data-line-number="4"><span class="co">#&gt; [1] TRUE</span></a>
<a class="sourceLine" id="cb31-5" data-line-number="5"><span class="kw">assert_that</span>(<span class="kw">is_single_numeric</span>(<span class="fl">1.6</span>))</a>
<a class="sourceLine" id="cb31-6" data-line-number="6"><span class="co">#&gt; [1] TRUE</span></a>
<a class="sourceLine" id="cb31-7" data-line-number="7"><span class="kw">assert_that</span>(<span class="kw">is_single_numeric</span>(<span class="ot">NA</span>))</a>
<a class="sourceLine" id="cb31-8" data-line-number="8"><span class="co">#&gt; Error: NA is not a single numeric value</span></a>
<a class="sourceLine" id="cb31-9" data-line-number="9"><span class="kw">assert_that</span>(<span class="kw">is_single_numeric</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">6</span>))</a>
<a class="sourceLine" id="cb31-10" data-line-number="10"><span class="co">#&gt; Error: 1:6 is not a single numeric value</span></a>
<a class="sourceLine" id="cb31-11" data-line-number="11"><span class="kw">assert_that</span>(<span class="kw">is_single_numeric</span>(<span class="st">&quot;pie&quot;</span>))</a>
<a class="sourceLine" id="cb31-12" data-line-number="12"><span class="co">#&gt; Error: &quot;pie&quot; is not a single numeric value</span></a></code></pre></div>
<h3 id="is_bounded-check-whether-a-value-within-a-certain-interval"><code>is_bounded</code>: Check whether a value within a certain interval</h3>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb32-1" data-line-number="1"><span class="kw">assert_that</span>(<span class="kw">is_bounded</span>(<span class="dv">10</span>))</a>
<a class="sourceLine" id="cb32-2" data-line-number="2"><span class="co">#&gt; [1] TRUE</span></a>
<a class="sourceLine" id="cb32-3" data-line-number="3"><span class="kw">assert_that</span>(<span class="kw">is_bounded</span>(<span class="dv">10</span><span class="op">:</span><span class="dv">30</span>))</a>
<a class="sourceLine" id="cb32-4" data-line-number="4"><span class="co">#&gt; [1] TRUE</span></a>
<a class="sourceLine" id="cb32-5" data-line-number="5"><span class="kw">assert_that</span>(<span class="kw">is_bounded</span>(<span class="ot">Inf</span>))</a>
<a class="sourceLine" id="cb32-6" data-line-number="6"><span class="co">#&gt; Error: Inf is not bounded by (-Inf,Inf)</span></a>
<a class="sourceLine" id="cb32-7" data-line-number="7"><span class="kw">assert_that</span>(<span class="kw">is_bounded</span>(<span class="dv">10</span>, <span class="dt">lower_bound =</span> <span class="dv">20</span>))</a>
<a class="sourceLine" id="cb32-8" data-line-number="8"><span class="co">#&gt; Error: 10 is not bounded by (20,Inf)</span></a>
<a class="sourceLine" id="cb32-9" data-line-number="9"><span class="kw">assert_that</span>(<span class="kw">is_bounded</span>(</a>
<a class="sourceLine" id="cb32-10" data-line-number="10">  <span class="dv">10</span>,</a>
<a class="sourceLine" id="cb32-11" data-line-number="11">  <span class="dt">lower_bound =</span> <span class="dv">20</span>,</a>
<a class="sourceLine" id="cb32-12" data-line-number="12">  <span class="dt">lower_closed =</span> <span class="ot">TRUE</span>,</a>
<a class="sourceLine" id="cb32-13" data-line-number="13">  <span class="dt">upper_bound =</span> <span class="dv">30</span>,</a>
<a class="sourceLine" id="cb32-14" data-line-number="14">  <span class="dt">upper_closed =</span> <span class="ot">FALSE</span></a>
<a class="sourceLine" id="cb32-15" data-line-number="15">))</a>
<a class="sourceLine" id="cb32-16" data-line-number="16"><span class="co">#&gt; Error: 10 is not bounded by [20,30)</span></a></code></pre></div>

</body>
</html>
